/**
 * dbms-sjtu
 * StartUp.java created at Jul 22, 2011
 */
package dbms.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import dbms.Debug;
import dbms.metadata.MasterPage;

/**
 * The main entry
 * @author weapon
 */
public class StartUp {
	public static void main(String[] args) throws IOException, InterruptedException{
		if (args.length != 1) {
			Debug.Error("no input database file");
		} else {
			/**
			 * init the buffer pool
			 */
			DbmsServer.init(args[0]);

			Debug.P("SQLsjk Database Version 1.0\n"
					+ "Enter help to get more information.");
			Debug.P("Enter SQL statements terminated with a \";\"");
			
			/**
			 * the main loop
			 */
			while (true) {
				System.out.print("sjkV5 > ");
				BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
				String str = inp.readLine();
				if (str.equals("exit")){
					break;
				} else if(str.equals("show tables")){
					for(String table : DbmsServer.masterPage.tableMap.keySet()){
						Debug.P(table);
					}
				} else if(str.startsWith("schema ")){
					if(str.split(" ").length >= 2){
						String table = str.split(" ")[1].trim();
						if(DbmsServer.masterPage.tableMap.containsKey(table)){
							Debug.P(DbmsServer.masterPage.transSQLBySchema(table, DbmsServer.masterPage.tableMap.get(table).table.getTableSchema()));
						} else if(DbmsServer.masterPage.viewMap.containsKey(table)){
							Debug.P(DbmsServer.masterPage.viewMap.get(table).viewSQL);
						} else {
							Debug.P("No this table or view.");
						}
						
					} else {
						Debug.Error("Wrong command.");
					}
				} else if(str.equals("info")){
					Debug.P("Version: " + MasterPage.VERSION);
					Debug.P("Page size: " + MasterPage.pageSize);
					Debug.P("firstFreePageNum: " + MasterPage.freeListPageNum);
					//FIXME
				} else if(str.equals("help")){
					DbmsServer.showHelp();
				} else {
					String resString = DbmsServer.basicExecuteDbms(str);
					if(resString != null){
						String[] responseLine = resString.split("#");
						for(int i = 0; i < responseLine.length; i++){
							System.out.println(responseLine[i]);
						}
					}
					
				}
				
				Thread.sleep(10);
			}
			DbmsServer.shutdown();
		}
	}
	
	
	public static String dbmsServerExecute(String str) throws InterruptedException{
		String result = "";
		if (str.equals("exit")){
			DbmsServer.shutdown();
		} else if(str.equals("show tables")){
			for(String table : DbmsServer.masterPage.tableMap.keySet()){
				result += table + "#";
			}
		} else if(str.startsWith("schema ")){
			if(str.split(" ").length >= 2){
				String table = str.split(" ")[1].trim();
				if(DbmsServer.masterPage.tableMap.containsKey(table)){
					result = DbmsServer.masterPage.transSQLBySchema(table, DbmsServer.masterPage.tableMap.get(table).table.getTableSchema());
				} else if(DbmsServer.masterPage.viewMap.containsKey(table)){
					result = DbmsServer.masterPage.viewMap.get(table).viewSQL;
				} else {
					result = "No this table or view.";
				}
				
			} else {
				result = "Wrong command.";
			}
		} else if(str.equals("info")){
			result += "Version: " + MasterPage.VERSION;
			result += "#Page size: " + MasterPage.pageSize;
			result += "#firstFreePageNum: " + MasterPage.freeListPageNum;
		} else if(str.equals("help")){
			result = "Enter the following commands to get more information: #" +
			"help		get information about this#" +
			"info		show information about database file#" +
			"schema	(table name)		show this table's schema information#" +
			"show tables		show all table name which stores in this database file#" +
			"exit		shutdown the system.";
		} else {
			result = DbmsServer.basicExecuteDbms(str);
		}
		
		return result;
	}
}
